package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.ListArray;
import com.diyizu.youdianda.dto.ListChildren;
import com.diyizu.youdianda.dto.SysConfigDto;
import com.diyizu.youdianda.entity.SysConfig;
import com.diyizu.youdianda.service.SysConfigService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_config")
public class SysconfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/all")
    public R<SysConfigDto> getAll(){
        SysConfigDto sysConfigDto = new SysConfigDto();
        //list
        //base
        ListArray baseList = new ListArray();
        baseList.setName("基础配置");
        baseList.setKey("base");

        ListChildren baseChildren1 = new ListChildren();
        baseChildren1.setName("站点配置");
        LambdaQueryWrapper<SysConfig> baseQw1 = new LambdaQueryWrapper<>();
        baseQw1.eq(SysConfig::getGroupValue, "站点配置");
        baseQw1.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> zhanDian = sysConfigService.list(baseQw1);

        List<SysConfigDto> zhanDian2 = zhanDian.stream().map(item->{
            SysConfigDto configDto = new SysConfigDto();
            BeanUtils.copyProperties(item,configDto);

            if (Strings.isNotEmpty(item.getConfigValue())){
                ArrayList<Map> list = new ArrayList<>();
                String sub = item.getConfigValue();
                //String substring = sub1.substring(2, roleId.length() - 2);
                String[] split = sub.split("&");
                for (String s : split) {
                    String[] strings = s.split("=");
                    Map<String, String> map = new HashMap<>();
                    map.put("label",strings[0]);
                    map.put("value",strings[1]);
                    list.add(map);
                }
                configDto.setConfig_value(list);
            }

            return configDto;
        }).collect(Collectors.toList());

        baseChildren1.setData(zhanDian2);

        ListChildren baseChildren2 = new ListChildren();
        baseChildren2.setName("底部信息");
        LambdaQueryWrapper<SysConfig> baseQw2 = new LambdaQueryWrapper<>();
        baseQw2.eq(SysConfig::getGroupValue, "底部信息");
        baseQw2.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> diBu = sysConfigService.list(baseQw2);
        baseChildren2.setData(diBu);

        ListChildren baseChildren3 = new ListChildren();
        baseChildren3.setName("其他配置");
        LambdaQueryWrapper<SysConfig> baseQw3 = new LambdaQueryWrapper<>();
        baseQw3.eq(SysConfig::getGroupValue, "其他配置");
        baseQw3.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> qiTa = sysConfigService.list(baseQw3);
        baseChildren3.setData(qiTa);

        ArrayList<ListChildren> BaseChildrens = new ArrayList<>();
        BaseChildrens.add(baseChildren1);
        BaseChildrens.add(baseChildren2);
        BaseChildrens.add(baseChildren3);
        baseList.setChildren(BaseChildrens);

        //system
        ListArray systemList = new ListArray();
        systemList.setName("系统配置");
        systemList.setKey("system");

        ListChildren systemChildren1 = new ListChildren();
        systemChildren1.setName("管理系统配置");
        LambdaQueryWrapper<SysConfig> systemQw1 = new LambdaQueryWrapper<>();
        systemQw1.eq(SysConfig::getGroupValue, "管理系统配置");
        systemQw1.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> guanLi = sysConfigService.list(systemQw1);
        systemChildren1.setData(guanLi);

        ArrayList<ListChildren> systemChildrens = new ArrayList<>();
        systemChildrens.add(systemChildren1);
        systemList.setChildren(systemChildrens);

        //custom
        ListArray customList = new ListArray();
        customList.setName("其他设置");
        customList.setKey("custom");

        ListChildren customChildren1 = new ListChildren();
        customChildren1.setName("通知公告");
        LambdaQueryWrapper<SysConfig> customQw1 = new LambdaQueryWrapper<>();
        customQw1.eq(SysConfig::getGroupValue, "通知公告");
        customQw1.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> tongZhi = sysConfigService.list(customQw1);
        customChildren1.setData(tongZhi);

        ListChildren customChildren2 = new ListChildren();
        customChildren2.setName("前台图片");
        LambdaQueryWrapper<SysConfig> customQw2 = new LambdaQueryWrapper<>();
        customQw2.eq(SysConfig::getGroupValue, "前台图片");
        customQw2.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> qianTai = sysConfigService.list(customQw2);
        customChildren2.setData(qianTai);

        ArrayList<ListChildren> customChildrens = new ArrayList<>();
        customChildrens.add(customChildren1);
        customChildrens.add(customChildren2);
        customList.setChildren(customChildrens);

        ArrayList<ListArray> list = new ArrayList<>();
        list.add(baseList);
        list.add(systemList);
        list.add(customList);
        sysConfigDto.setList(list);

        //allData
        LambdaQueryWrapper<SysConfig> allDataQw = new LambdaQueryWrapper<>();
        allDataQw.orderByAsc(SysConfig::getSortNum);
        List<SysConfig> sysConfigs = sysConfigService.list(allDataQw);
        sysConfigDto.setAllData(sysConfigs);

        //configForm
        HashMap<String, String> configForm = new HashMap<>();
        for (SysConfig sysConfig : sysConfigs) {
            String attrKey = sysConfig.getAttrKey();
            String attrValue = sysConfig.getAttrValue();
            configForm.put(attrKey,attrValue);
        }
        sysConfigDto.setConfigForm(configForm);

        //configRules
        HashMap<String, String> configRule = new HashMap<>();
        for (SysConfig sysConfig : sysConfigs) {
            String validator = sysConfig.getValidator();
            if (Strings.isNotEmpty(validator)){
                String attrKey = sysConfig.getAttrKey();
                configRule.put(attrKey,validator);
            }
        }
        sysConfigDto.setConfigRules(configRule);

        return R.success(sysConfigDto);
    }

    @PostMapping("/update")
    public R update(@RequestBody List<SysConfig> configList){
        sysConfigService.updateBatchById(configList);
        return R.success("系统配置更新成功");
    }


}
